package com.shzu.jwc.ty.service;

import com.shzu.jwc.ty.bean.PeExaminationAppointment;
import com.shzu.jwc.ty.bean.PeExaminationAppointmentExample;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.dao.PeExaminationAppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PeExaminationScoreservice {

	@Autowired
	StudentInfoservice studentInfoservice;
	
	@Autowired
	SystemYearservice systemYearservice;
	
	@Autowired
	PeExaminationAppointmentMapper mapper;

	@Autowired
	SystemConfigservice SystemConfigservice;

	@Autowired
	SystemConfigservice systemConfigservice;

	public Integer countexaminationscore(Integer value){
		String systenSelectcourseCode=null;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		return (int)mapper.countpeExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),systenSelectcourseCode);
	}

	public Integer countselectexaminationscore(Integer value,Integer peExaminationId){
		if(peExaminationId==0)
			peExaminationId=null;
		String systenSelectcourseCode=null;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		return (int)mapper.countselectpeExamination(systemYearservice.getoneyearby(Semester.getnow()),systenSelectcourseCode,peExaminationId);
	}
	public List<PeExaminationAppointment> getexaminationscore(Integer value, Page page){

//		PeExaminationAppointmentExample example=new PeExaminationAppointmentExample();
//		PeExaminationAppointmentExample.Criteria criteria=example.createCriteria();
//		criteria.andSystemYearIdEqualTo(systemYearservice.getoneyearby(Semester.getnow()));
//		criteria.andPeExaminationAppointmentScoreIsNotNull();
//		criteria.andPeExaminationAppointmentScoreGreaterThanOrEqualTo(60.0);
//		criteria.andGetAccess(value);
		String systenSelectcourseCode=null;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		return mapper.getpeExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),systenSelectcourseCode,page.getStartPos(),page.getPageSize());
	}

	public List<PeExaminationAppointment> selectexaminationscore(Integer peExaminationId,Integer value,Page page){
		if(peExaminationId==0)
			peExaminationId=null;
		String systenSelectcourseCode=null;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		return mapper.selectpeExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),systenSelectcourseCode,peExaminationId,page.getStartPos(),page.getPageSize());
	}

	public List<PeExaminationAppointment> upselectexaminationscore(Integer userStudentInfoYear,String userStudentInfoCollege,Integer peExaminationId,Integer value){
		String systenSelectcourseCode=null;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		if(userStudentInfoYear==0)
			userStudentInfoYear=null;
		if(peExaminationId==0)
			peExaminationId=null;
		if(userStudentInfoCollege.equals("0"))
			userStudentInfoCollege=null;
		return mapper.upselectpeExaminationscore(systemYearservice.getoneyearby(Semester.getnow()),systenSelectcourseCode,userStudentInfoYear,userStudentInfoCollege,peExaminationId);
	}
	
	public List<PeExaminationAppointment> searchexaminationscore(String userStudentInfoNum,Integer value){
		String systenSelectcourseCode=null;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		PeExaminationAppointmentExample example=new PeExaminationAppointmentExample();
		PeExaminationAppointmentExample.Criteria criteria=example.createCriteria();
		criteria.andSystemYearIdEqualTo(systemYearservice.getoneyearby(Semester.getnow()));
		criteria.andIsSubmitEqualTo("readonly");
		criteria.andSystenSelectcourseCodeEqualTo(systenSelectcourseCode);
		criteria.andUserStudentInfoNumEqualTo(userStudentInfoNum);
		criteria.andPeExaminationAppointmentStateEqualTo(1);
		return mapper.selectByExample(example);
	}

	public List<PeExaminationAppointment> getTotelScore(Integer value){
		String systenSelectcourseCode=null;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		PeExaminationAppointmentExample example=new PeExaminationAppointmentExample();
		PeExaminationAppointmentExample.Criteria criteria=example.createCriteria();
		criteria.andSystemYearIdEqualTo(systemYearservice.getoneyearby(Semester.getnow()));
		criteria.andIsSubmitEqualTo("readonly");
		criteria.andSystenSelectcourseCodeEqualTo(systenSelectcourseCode);
		criteria.andPeExaminationAppointmentStateEqualTo(1);
		return mapper.selectByExample(example);
	}

	public Integer updatestatusbyexamination(Integer peExaminationAppointmentState,Integer peExaminationId){
		return mapper.updatestatusbyexamination(peExaminationAppointmentState,peExaminationId);
	}

	public Integer updatescore(double peExaminationAppointmentScore,Integer peExaminationAppointmentId){
		return mapper.updatescore(peExaminationAppointmentScore,peExaminationAppointmentId);
	}

	public Double updateMultscore(String name,Double score,Integer peExaminationAppointmentId){
		if(name.equals("peExaminationAppointmentScore"))
			mapper.updatescore(score,peExaminationAppointmentId);
		else if(name.equals("peExaminationNormalscore")) {
			mapper.updatenormalscore(score,peExaminationAppointmentId);
		}
		else
			mapper.updatemidscore(score,peExaminationAppointmentId);
		PeExaminationAppointment appointment=mapper.selectByPrimaryKey(peExaminationAppointmentId);
		Double Totalscore;
		if(appointment.getPeExaminationAppointmentScore()==null || appointment.getPeExaminationNormalscore()==null || appointment.getPeExaminationMidscore()==null)
			Totalscore= null;
		else {
			Double percentageNormal = Double.parseDouble(systemConfigservice.getvalue("pe_examination_normal")) * 1.0 / 100;
			Double percentageMid = Double.parseDouble(systemConfigservice.getvalue("pe_examination_mid")) * 1.0 / 100;
			Double percentageScore = Double.parseDouble(systemConfigservice.getvalue("pe_examination_end")) * 1.0 / 100;
			Totalscore = appointment.getPeExaminationNormalscore() * percentageNormal + appointment.getPeExaminationMidscore() * percentageMid + appointment.getPeExaminationAppointmentScore() * percentageScore;
		}
		mapper.updatetotalscore(Totalscore,peExaminationAppointmentId);
		return Totalscore;
	}

	public List<PeExaminationAppointment> getData(Integer systemYearId, Double peExaminationAppointmentScore, Integer peExaminationId, String userStudentInfoNum,Integer value){
		String systenSelectcourseCode;
		if(value==1)
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillsone");
		else
			systenSelectcourseCode=SystemConfigservice.getvalue("sportsskillstwo");
		return mapper.getData(systemYearId,peExaminationAppointmentScore,peExaminationId,userStudentInfoNum,systenSelectcourseCode);
	}
}
